package com.gwq.computed;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class OperationBase {

    static final int UPPER = 100;
    static final int LOWER = 0;




    //生成加减法算式基
    public BinaryOperation2[][]  produceOperationBase (){
        BinaryOperation2[][]base = new BinaryOperation2[UPPER+1][UPPER+1];
        BinaryOperation2 ao;
        for(int i=0;i<=UPPER;i++){
            for(int j=0;j<=UPPER;j++){
                if(i+j<=UPPER){
                    ao = new AdditionOperation();
                    ao.unsafeConstructor(i,j,i+j,'+');
                }else{
                    ao = new SubstractOperation();
                    ao.unsafeConstructor(i,UPPER-j,i-(UPPER-j),'-');
                }
                base[i][j] = ao;
            }
        }
        return base;
    }


    //加法算式基
    AdditionOperation[][]  produceAdditionBase (){
        AdditionOperation[][]base = new AdditionOperation[UPPER+1][UPPER+1];
        AdditionOperation ao;
        for(int i=0;i<=UPPER;i++){
            for(int j=0;j<=UPPER;j++){
                if(i+j<=UPPER){
                    ao = new AdditionOperation();
                    base[i][j] = ao;
                    ao.unsafeConstructor(i,j,i+j,'+');
                }
            }
        }
        return base;
    }

    //减法算式基
    SubstractOperation[][]  produceSubtionBase(){
        SubstractOperation[][]base = new SubstractOperation[UPPER+1][UPPER+1];
        SubstractOperation ao;
        for(int i=0;i<=UPPER;i++){
            for(int j=0;j<=UPPER;j++){
                if(i-j>=LOWER){
                    ao = new SubstractOperation();
                    base[i][j] = ao;
                    ao.unsafeConstructor(i,j,i-j,'-');
                }
            }
        }
        return base;
    }



    public void displayBase(BinaryOperation2[][] base){
        for(BinaryOperation2[] row: base){
            for(BinaryOperation2 operation: row){
                if(operation!=null){
                    System.out.print(operation.asString()+"\t");
                }
            }
            System.out.println();
        }
    }



    //根据count数随机选取count道题
    private Exercise2 generateExercise(int count,BinaryOperation2[][] base){
        BinaryOperation2 ao;
        Exercise2 exercise2 = new Exercise2();
        Random random = new Random();
        int row,column;
        while(count>0){
            do{
                row = random.nextInt(UPPER+1);
                column = random.nextInt(UPPER+1);
                ao = base[row][column];
            }while(ao==null||exercise2.contains(ao,count));
            exercise2.add(ao);
            count--;
        }
        return  exercise2;
    }


    //习题
    public Exercise2 generateAdditionExercise(int count){
        OperationBase ob = new OperationBase();
        BinaryOperation2[][] base = ob.produceOperationBase();
        return generateExercise(count,base);
    }

}
